package com.wing.productself.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.productself.model.entity.ProductCategorySelf;
import com.wing.productself.model.response.ProductCategorySelfVO;
import com.wing.productself.service.ProductCategorySelfService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.productself.model.request.ProductCategorySelfForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-产品分类管理", tags = "产品分类管理")
@RestController("appProductCategorySelfController")
@RequestMapping("productself/app/productCategorySelf")
public class ProductCategorySelfController {

    @Autowired
    private ProductCategorySelfService productCategorySelfService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<ProductCategorySelfVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {
        Page<ProductCategorySelfVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<ProductCategorySelfVO> list = productCategorySelfService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long productCategorySelfId) {
        ProductCategorySelfVO productCategorySelfVO = productCategorySelfService.get4appById(productCategorySelfId);
        if (productCategorySelfVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), productCategorySelfVO);
    }

    @PostMapping()
    public JsonResult add(@Validated ProductCategorySelfForm productCategorySelfForm) {
        if (productCategorySelfService.add(productCategorySelfForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
